package com.GameSpace.CoreAssets;

import java.util.HashMap;
import java.util.prefs.Preferences;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class SoundManager {
	public static boolean isSoundsEnabled() {
		Preferences prefs = Preferences.userNodeForPackage(SoundManager.class);
		return prefs.getBoolean("sounds", true);
	}
	public static void setSoundsEnabled(boolean enabled) {
		Preferences prefs = Preferences.userNodeForPackage(SoundManager.class);
		prefs.putBoolean("sounds", enabled);
	}
	private AudioManager mAudioManager;
	private Context mContext;

	private SoundPool mSoundPool;

	private HashMap<String, Integer> mSoundPoolMap;

	public void addSound(String key, int SoundID) {
		mSoundPoolMap.put(key, mSoundPool.load(mContext, SoundID, 1));
	}

	public void initSounds(Context theContext) {
		mContext = theContext;
		mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
		mSoundPoolMap = new HashMap<String, Integer>();
		mAudioManager = (AudioManager) mContext
				.getSystemService(Context.AUDIO_SERVICE);
	}

	public void playSound(String key) {
		if (!isSoundsEnabled()) {
			return;
		}
		float streamVolume = mAudioManager
				.getStreamVolume(AudioManager.STREAM_MUSIC);
		streamVolume = streamVolume
				/ mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		mSoundPool.play(mSoundPoolMap.get(key), streamVolume, streamVolume, 1,
				0, 1f);
	}
}
